/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.desk;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import com.ruipengkj.bean.TExePosCatalog;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonDishType;
import com.ruipengkj.component.jpanel.ImagePanel;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.factory.UIFactory;
import com.ruipengkj.service.TExePosCatalogService;
/**
 * 说明：
 * 餐桌类型面板
 */
public class DeskTypePanel extends ImagePanel implements ActionListener, Runnable{
	private static final long serialVersionUID = 1L;
	
	
	//菜品类型 service
	private TExePosCatalogService TExePosCatalogService = BuildSpringApplication.getInstance().getTExePosCatalogServiceImpl();
	
	private JButton prevButton;//上一个
	private JButton nextButton;//下一页
	private ImagePanel prevPanel = new ImagePanel(null);//上一个
	private ImagePanel nextPanel = new ImagePanel(null);//下一页
	
	private JPanel mainPanel = UIFactory.getInstance().createBoxJPanel(BoxLayout.X_AXIS);
	
	private List<TExePosCatalog> TExePosCatalogList = null;
	
	private JToggleButton noDeskButton;
	private String selectTExePosCatalog;//选中的餐桌类型ID
	
	public DeskTypePanel() {
		Util.setSize(this, 40, 40);
		this.setLayout(new BorderLayout());
		this.setImage(ImageFactory.loadImage(ImageFactory.PANEL_DISH_TYPE_BG).getImage());
		init();
//		
		this.addComponentListener(new sizeComponentListener());
	}
	
	private void init() {
		prevButton = UIFactory.getInstance().createJButton(ImageFactory.loadImage(ImageFactory.BTN_TYPE_PREV), this, false, false);
		nextButton = UIFactory.getInstance().createJButton(ImageFactory.loadImage(ImageFactory.BTN_TYPE_NEXT), this, false, false);
		
		prevButton.setRolloverIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_PREV_CLICK));
		prevButton.setRolloverSelectedIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_PREV_CLICK));
		nextButton.setRolloverIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_NEXT_CLICK));
		nextButton.setRolloverSelectedIcon(ImageFactory.loadImage(ImageFactory.BTN_TYPE_NEXT_CLICK));
//		prevPanel.add(prevButton);
//		nextPanel.add(nextButton);
		
		prevPanel.setOpaque(false);
		nextPanel.setOpaque(false);
//		nextPanel.setImage(ImageFactory.loadImage(ImageFactory.PANEL_DISH_TYPE_BG).getImage());
		
		Util.setSize(prevPanel, 50, 40);
		Util.setSize(nextPanel, 50, 40);
		
		prevButton.setBounds(5, 0, 40, 40);
		nextButton.setBounds(5, 0, 40, 40);

		noDeskButton = new RPJButtonDishType("没有餐桌");
		noDeskButton.addActionListener(this);
		
		termQuery();
	}

	//条件查询
	public void termQuery(){
//		new Thread(){
//			public void run(){
				try {
					x = 0;y = 0;
					dispose();
					mainPanel.removeAll();
					TExePosCatalogList = TExePosCatalogService.findService(null);
					if(TExePosCatalogList != null){
						addButton();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
//			}
//		}.start();
	}
	
	private int x = 0, y = 0;
	private int width = 23800;
	private void addButton() {
		JPanel jp = new JPanel(null);
		jp.setOpaque(false);
		jp.add(mainPanel);
		
		ButtonGroup bg = new ButtonGroup();
		JToggleButton jb2 = new RPJButtonDishType("所有餐桌");
		jb2.addActionListener(this);
		
		mainPanel.add(noDeskButton);
		mainPanel.add(jb2);
		bg.add(jb2);
		for (int i = 0, length = TExePosCatalogList.size(); i < length; ++i) {
			TExePosCatalog tExePosCatalog = TExePosCatalogList.get(i);
			JToggleButton jb = new RPJButtonDishType(tExePosCatalog.getName());
			jb.setName(tExePosCatalog.getPosCatalogID());
			jb.addActionListener(this);
			mainPanel.add(jb);
			bg.add(jb);
		}
		
		mainPanel.setBounds(x, y, width, 40);
		this.add(jp);
		
		this.add(prevPanel, BorderLayout.WEST);
		this.add(nextPanel, BorderLayout.EAST);
		this.updateUI();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(new Color(234, 99, 0));
		g.drawLine(0, 0, 0, this.getHeight());
		g.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight());
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == nextButton) {
			prevPanel.add(prevButton);
			x -= 200;
			mainPanel.setBounds(x, y, width, 40);
			if (mainPanel.getComponentCount()-1 >= 0) {
				Rectangle rect = mainPanel.getComponent(mainPanel.getComponentCount()-1).getBounds();
				if (DeskTypePanel.this.getWidth() - prevPanel.getWidth() - nextPanel.getWidth() >= rect.x+rect.width+x) {
					nextPanel.remove(0);
				}
			}
			this.updateUI();
		} else if (e.getSource() == prevButton) {
			nextPanel.add(nextButton);
			x += 200;
			mainPanel.setBounds(x, y, width, 40);
			if (x >= 0) {
				prevPanel.remove(prevButton);
			}
			this.updateUI();
		} else if (e.getSource() == noDeskButton) {
			setNoDesk();
		} else {
			for (int i = mainPanel.getComponentCount() - 1; i >= 0; --i) {
				//点击了菜品分类按钮
				if (e.getSource() == mainPanel.getComponent(i)) {
					selectTExePosCatalog = ((JToggleButton)e.getSource()).getName();
					QueryDesk(((JToggleButton)e.getSource()).getName());
					return;
				}
			}
		}
	}
	
	/**回调函数：参数餐桌类型。根据类型查询桌子*/
	public void QueryDesk(String CatalogId) {
	}
	
	/**回调函数，当前选中的是，没有桌子*/
	protected void setNoDesk() {
	}
	
	/**返回选中的类型ID*/
	public String getSelectTExePosCatalog() {
		return selectTExePosCatalog;
	}

	public JToggleButton getNoDeskButton() {
		return noDeskButton;
	}

	private void dispose() {
		if (mainPanel.getComponentCount()-1 >= 0) {
			Rectangle rect = mainPanel.getComponent(mainPanel.getComponentCount()-1).getBounds();
			if (this.getWidth() - prevPanel.getWidth() - nextPanel.getWidth() < rect.x+rect.width) {
				nextPanel.add(nextButton);
				this.updateUI();
			}
		}
	}
		
	class sizeComponentListener implements ComponentListener{
		//组件大小更改时调用。
		@Override
		public void componentResized(ComponentEvent e) {
			dispose();
		}
		
		@Override
		public void componentHidden(ComponentEvent arg0) {}
		@Override
		public void componentMoved(ComponentEvent arg0) {}
		@Override
		public void componentShown(ComponentEvent e) {}
	}

	@Override
	public void run() {
		termQuery();
		dispose();
	}
}
